﻿using DataBaseConnect.IRepositories;
using Entity;
using Entity.Dto;
using System;
using System.Collections.Generic;
using Utils;

namespace DataBaseConnect.App
{
    /// <summary>
    /// 用户管理服务
    /// </summary>
    public class UserAppService : IUserAppService
    {
        //用户管理仓储接口
        private readonly IUserRepository _userReporitory;

        /// <summary>
        /// 构造函数 实现依赖注入
        /// </summary>
        /// <param name="userRepository">仓储对象</param>
        public UserAppService(IUserRepository userRepository)
        {
            _userReporitory = userRepository;
        }

        /// <summary>
        /// 根据用户名和密码获取用户信息
        /// </summary>
        /// <param name="Name">用户名</param>
        /// <param name="Passowrd">密码</param>
        /// <returns></returns>
        public User GetUser(string Name, string Passowrd)
        {
            return _userReporitory.GetUser(Name, Passowrd);
        }

        /// <summary>
        /// 获取所有用户&&查询
        /// </summary>
        /// <returns></returns>
        public List<User> GetUserList(string UserName)
        {
            if (!string.IsNullOrEmpty(UserName))
            {
                return _userReporitory.GetAllList(f => f.UserName == UserName);
            }
            else
            {
                return _userReporitory.GetAllList();
            }
        }

        /// <summary>
        /// 新增用户信息
        /// </summary>
        /// <param name="Input">用户信息实体</param>
        /// <returns></returns>
        public ReceiveStatus AddUserMsg(User Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _userReporitory.Insert(Input, true);
                receiveStatus.msg = "新增成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 根据主键获取用户
        /// </summary>
        /// <param name="UserId">主键</param>
        /// <returns></returns>
        public User GetUserFirst(int UserId)
        {
            return _userReporitory.FirstOrDefault(f => f.UserId == UserId);
        }

        /// <summary>
        /// 编辑用户信息
        /// </summary>
        /// <param name="Input">用户信息实体</param>
        /// <returns></returns>
        public ReceiveStatus EditUserMsg(User Input)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _userReporitory.Update(Input);
                receiveStatus.msg = "编辑成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 根据主键删除用户信息
        /// </summary>
        /// <param name="UserId">用户主键</param>
        /// <returns></returns>
        public ReceiveStatus DelUserMsg(int UserId)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                _userReporitory.Delete(f => f.UserId == UserId, true);
                receiveStatus.msg = "删除成功";
            }
            catch (Exception ex)
            {
                receiveStatus.code = statuCode.Error;
                receiveStatus.msg = ex.Message;
            }
            return receiveStatus;
        }

        /// <summary>
        /// 获取所有用户信息（包括部门名称）&&查询
        /// </summary>
        /// <param name="UserName">用户名称</param>
        /// <returns></returns>
        public List<UserDto> GetUserOrDepartment(string UserName)
        {
            return _userReporitory.GetUserOrDepartment(UserName);
        }
    }
}
